package com.lmiot.xyclick.Share;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lmiot.floatviewsdklibrary.StateBarUtil;
import com.lmiot.xyclick.Activity.BaseActivity;
import com.lmiot.xyclick.App.MyApp;
import com.lmiot.xyclick.Bean.SQL.SearchBean;
import com.lmiot.xyclick.Bean.SQL.SearchBeanSqlUtil;
import com.lkx.liandianqinew.R;
import com.lmiot.xyclick.Share.inteface.OnSearchListener;
import com.lmiot.xyclick.Util.TimeUtils;
import com.lmiot.xyclick.View.MyGridView;
import com.xiaoyi.intentsdklibrary.Bean.ShareBean;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;
import com.youyi.yyviewsdklibrary.Dialog.util.ToastUtil;
import java.util.List;

public class SearchShareActivity extends BaseActivity implements View.OnClickListener {
    private boolean mDelFlag;
    MyGridView mIdHistoryGridview;
    ImageView mIdHistroyClear;
    TextView mIdHistroyClearAll;
    TextView mIdHistroyClearDone;
    LinearLayout mIdHistroyLayout;
    ImageView mIdSearchBack;
    ImageView mIdSearchClear;
    EditText mIdSearchEdit;
    RelativeLayout mIdSearchLayout;
    ImageView mIdSearchSearch;
    private String mKeyword;
    RecyclerView mRecyclerView;

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.SupportActivity, com.lmiot.xyclick.Activity.BaseActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_share_search);
        initView();
        StateBarUtil.immersive(this);
        StateBarUtil.setMargin(this, this.mIdSearchLayout);
        setEdit();
        showHistory();
        this.mRecyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.transparent), -1, 20, 99));
        this.mRecyclerView.setLayoutManager(new LinearLayoutManager(this, 1, false));
        this.mRecyclerView.setFocusable(false);
        String stringExtra = getIntent().getStringExtra("keyword");
        this.mKeyword = stringExtra;
        if (!TextUtils.isEmpty(stringExtra)) {
            this.mIdSearchEdit.setText(this.mKeyword);
            searchKeyWord(this.mKeyword);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void showHistory() {
        List<SearchBean> searchAll = SearchBeanSqlUtil.getInstance().searchAll();
        if (searchAll.size() > 0) {
            this.mIdHistroyLayout.setVisibility(View.VISIBLE);
            this.mIdHistroyClear.setVisibility(View.VISIBLE);
            this.mIdHistroyClearAll.setVisibility(View.GONE);
            this.mIdHistroyClearDone.setVisibility(View.GONE);
            this.mIdHistoryGridview.setAdapter((ListAdapter) new HistoryAdapter(searchAll));
            return;
        }
        this.mIdHistroyLayout.setVisibility(View.GONE);
        this.mIdHistroyClear.setVisibility(View.GONE);
        this.mIdHistroyClearAll.setVisibility(View.GONE);
        this.mIdHistroyClearDone.setVisibility(View.GONE);
    }

    private void initView() {
        this.mIdSearchEdit = (EditText) findViewById(R.id.id_search_edit);
        this.mIdSearchBack = (ImageView) findViewById(R.id.id_search_back);
        this.mIdSearchClear = (ImageView) findViewById(R.id.id_search_clear);
        this.mIdSearchSearch = (ImageView) findViewById(R.id.id_search_search);
        this.mIdSearchLayout = (RelativeLayout) findViewById(R.id.id_search_layout);
        this.mIdHistroyClearAll = (TextView) findViewById(R.id.id_histroy_clear_all);
        this.mIdHistroyClearDone = (TextView) findViewById(R.id.id_histroy_clear_done);
        this.mIdHistroyClear = (ImageView) findViewById(R.id.id_histroy_clear);
        this.mIdHistroyLayout = (LinearLayout) findViewById(R.id.id_histroy_layout);
        this.mIdHistoryGridview = (MyGridView) findViewById(R.id.id_history_gridview);
        this.mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        this.mIdSearchBack.setOnClickListener(this);
        this.mIdSearchClear.setOnClickListener(this);
        this.mIdSearchSearch.setOnClickListener(this);
        this.mIdHistroyClearAll.setOnClickListener(this);
        this.mIdHistroyClearDone.setOnClickListener(this);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_histroy_clear:
                this.mIdHistroyClear.setVisibility(View.GONE);
                this.mIdHistroyClearAll.setVisibility(View.VISIBLE);
                this.mIdHistroyClearDone.setVisibility(View.VISIBLE);
                this.mDelFlag = true;
                this.mIdHistoryGridview.setAdapter((ListAdapter) new HistoryAdapter(SearchBeanSqlUtil.getInstance().searchAll()));
                return;
            case R.id.id_histroy_clear_all:
                this.mIdHistroyClear.setVisibility(View.VISIBLE);
                this.mIdHistroyClearAll.setVisibility(View.GONE);
                this.mIdHistroyClearDone.setVisibility(View.GONE);
                SearchBeanSqlUtil.getInstance().delAll();
                this.mDelFlag = false;
                this.mIdHistoryGridview.setAdapter((ListAdapter) new HistoryAdapter(SearchBeanSqlUtil.getInstance().searchAll()));
                return;
            case R.id.id_histroy_clear_done:
                this.mIdHistroyClear.setVisibility(View.VISIBLE);
                this.mIdHistroyClearAll.setVisibility(View.GONE);
                this.mIdHistroyClearDone.setVisibility(View.GONE);
                this.mDelFlag = false;
                showHistory();
                return;
            case R.id.id_search_back:
                finish();
                return;
            case R.id.id_search_clear:
                this.mIdSearchEdit.setText("");
                return;
            case R.id.id_search_search:
                String trim = this.mIdSearchEdit.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    ToastUtil.warning("搜索内容不能为空！");
                    return;
                } else {
                    searchKeyWord(trim);
                    return;
                }
            default:
                return;
        }
    }

    /* access modifiers changed from: private */
    public class HistoryAdapter extends BaseAdapter {
        private List<SearchBean> mList;

        public Object getItem(int i) {
            return null;
        }

        public long getItemId(int i) {
            return 0;
        }

        public HistoryAdapter(List<SearchBean> list) {
            this.mList = list;
        }

        public int getCount() {
            return this.mList.size();
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            View inflate = View.inflate(SearchShareActivity.this, R.layout.item_search_histroy, null);
            ImageView imageView = (ImageView) inflate.findViewById(R.id.id_del);
            final SearchBean searchBean = this.mList.get(i);
            ((TextView) inflate.findViewById(R.id.id_name)).setText(searchBean.getKeyWord());
            if (SearchShareActivity.this.mDelFlag) {
                imageView.setVisibility(View.VISIBLE);
            } else {
                imageView.setVisibility(View.GONE);
            }
            inflate.setOnClickListener(new View.OnClickListener() {
                /* class com.lmiot.xyclick.Share.SearchShareActivity.HistoryAdapter.AnonymousClass1 */

                public void onClick(View view) {
                    SearchShareActivity.this.searchKeyWord(searchBean.getKeyWord());
                }
            });
            imageView.setOnClickListener(new View.OnClickListener() {
                /* class com.lmiot.xyclick.Share.SearchShareActivity.HistoryAdapter.AnonymousClass2 */

                public void onClick(View view) {
                    SearchBeanSqlUtil.getInstance().delByID(searchBean.getKeyWord());
                    HistoryAdapter.this.mList = SearchBeanSqlUtil.getInstance().searchAll();
                    HistoryAdapter.this.notifyDataSetChanged();
                }
            });
            return inflate;
        }
    }

    private void setEdit() {
        this.mIdSearchEdit.addTextChangedListener(new TextWatcher() {
            /* class com.lmiot.xyclick.Share.SearchShareActivity.AnonymousClass1 */

            public void afterTextChanged(Editable editable) {
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (TextUtils.isEmpty(charSequence.toString())) {
                    SearchShareActivity.this.mIdSearchClear.setVisibility(View.GONE);
                } else {
                    SearchShareActivity.this.mIdSearchClear.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void searchKeyWord(String str) {
        SearchBeanSqlUtil.getInstance().add(new SearchBean(null, str, TimeUtils.getCurrentTime()));
        ShareSDK.getInstance().searchFileByName(MyApp.getContext().getPackageName(), str, new OnSearchListener() {
            /* class com.lmiot.xyclick.Share.SearchShareActivity.AnonymousClass2 */

            @Override // com.lmiot.xyclick.Share.inteface.OnSearchListener
            public void result(boolean z, List<ShareBean> list) {
                if (list.size() == 0) {
                    ToastUtil.warning("搜索结果为空！");
                }
                SearchShareActivity.this.mRecyclerView.setVisibility(View.VISIBLE);
                SearchShareActivity.this.mRecyclerView.setAdapter(new ShareAdapter(SearchShareActivity.this, list, false));
                SearchShareActivity.this.mRecyclerView.setNestedScrollingEnabled(false);
                SearchShareActivity.this.showHistory();
            }
        });
    }
}
